package com.FindMyStuff.Activities;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.FindMyStuff.Model.DataAccessor;
import com.FindMyStuff.Model.DataBaseHelper;
import com.FindMyStuff.Model.InputChecker;
import com.FindMyStuff.Model.Item;
import com.FindMyStuff.Model.ItemCategory;
import com.FindMyStuff.Model.SessionManager;

public class SearchActivity extends ListActivity {

	private SessionManager sm;
	private ArrayList<Item> values;
	private ArrayAdapter<Item> listAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setDisplayShowTitleEnabled(false);
		getActionBar().setIcon(getResources().getIdentifier("ic_home", "drawable",  getPackageName()));
		
		sm = SessionManager.getInstance();
		DataBaseHelper dbh = new DataBaseHelper(this);
		DataAccessor da = new DataAccessor(dbh);
		String name = getIntent().getStringExtra("seachText");
		
		if(name.length() == 0)
		{
			values = da.getAllItems();
		} else
		{
			values = da.getItemsByName(name);
		}
		
		setContentView(R.layout.activity_search);
		Spinner spinner = (Spinner) findViewById(R.id.item_cat);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
		        R.array.item_category_select, android.R.layout.simple_spinner_item);
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		
		listAdapter = new ArrayAdapter<Item>(this,
				android.R.layout.simple_list_item_1, values);
		setListAdapter(listAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent toHome = new Intent(this, HomeActivity.class);
			startActivity(toHome);
			overridePendingTransition  (R.anim.left_slide_out, R.anim.left_slide_in);
			return true;
		case R.id.menu_logout:
			Intent intent = new Intent(this, LogInActivity.class);
			sm.logout();
			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	/**
	 * Checks the database for the item matching the inputed category and name
	 * @param view
	 */
	public void search(View view)
	{
		DataBaseHelper dbh = new DataBaseHelper(this);
		DataAccessor da = new DataAccessor(dbh);
		EditText itemName = (EditText) findViewById(R.id.search_text);
		Spinner category_spinner = (Spinner) findViewById(R.id.item_cat);
		ItemCategory cat = ItemCategory.convertStringtoCategory
								(category_spinner.getSelectedItem().toString());
		if(cat.equals(ItemCategory.All))
		{
			values = da.getItemsByName(InputChecker.removeQuotes(itemName.getText().toString()));
		} else
		{
			values = da.getItemsByName(InputChecker.removeQuotes(itemName.getText().toString()), cat);
		}
		listAdapter = new ArrayAdapter<Item>(this,
				android.R.layout.simple_list_item_1, values);
		setListAdapter(listAdapter);
	}

}
